package com.hotchpotch.base;

import android.app.Application;
import android.content.Context;

import com.cretin.www.cretinautoupdatelibrary.utils.CretinAutoUpdateUtils;
import com.hotchpotch.R;
import com.hotchpotch.bean.UpdateModel;
import com.hotchpotch.retrofit.RetrofitManager;
import com.raizlabs.android.dbflow.config.FlowManager;

/**
 * Created by Administrator on 2017/5/11 0011.
 */

public class MyApplication extends Application {
    private static Context context;
    private ActivityManager activityManager = null; // activity管理类

    @Override
    public void onCreate() {
        super.onCreate();
        //导入DBFlow数据库
        FlowManager.init(this);
//        FlowManager.init(new FlowConfig.Builder(this).build());//两句都能配置
        context = getApplicationContext();
        activityManager = ActivityManager.getInstance(); // 获得实例
        // the following line is important(图片选择器用到的)
//        Fresco.initialize(getApplicationContext());
        CretinAutoUpdateUtils.Builder builder = new CretinAutoUpdateUtils.Builder()
                //设置更新api
                .setBaseUrl("http://101.201.31.212:8016/version/checkVersion")
                //设置是否显示忽略此版本
                .setIgnoreThisVersion(true)
                //设置下载显示形式 对话框或者通知栏显示 二选一
                .setShowType(CretinAutoUpdateUtils.Builder.TYPE_NITIFICATION)
                //设置下载时展示的图标
                .setIconRes(R.mipmap.ic_launcher)
                //设置是否打印log日志
                .showLog(true)
                //设置请求方式
                .setRequestMethod(CretinAutoUpdateUtils.Builder.METHOD_GET)
                //设置下载时展示的应用名称
                .setAppName("鸿邦商城")
                //设置自定义的Model类
                .setTransition(new UpdateModel())
                .build();
        CretinAutoUpdateUtils.init(builder);
        //初始化retrofit工具类
        RetrofitManager retrofitManager = RetrofitManager.getInstance();
        retrofitManager.init(getApplicationContext());
    }

    /**
     * 获得当前进程的名字
     * @return
     */
    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();
        android.app.ActivityManager activityManager = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (android.app.ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

    public static Context getContext() {
        return context;
    }

    private static final MyApplication mInstance = new MyApplication();

    public static MyApplication getInstance() {
        return mInstance;
    }

    public ActivityManager getActivityManager() {
        return activityManager;
    }
}
